package com.hsjk.szwj.module.smedia.service.production;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.AddEditingProjectMaterialsRequest;
import com.aliyun.ice20201109.models.AddEditingProjectMaterialsResponse;
import com.aliyun.ice20201109.models.CreateEditingProjectRequest;
import com.aliyun.ice20201109.models.CreateEditingProjectResponse;
import com.aliyun.ice20201109.models.DeleteEditingProjectMaterialsRequest;
import com.aliyun.ice20201109.models.DeleteEditingProjectMaterialsResponse;
import com.aliyun.ice20201109.models.DeleteEditingProjectsRequest;
import com.aliyun.ice20201109.models.DeleteEditingProjectsResponse;
import com.aliyun.ice20201109.models.GetEditingProjectMaterialsRequest;
import com.aliyun.ice20201109.models.GetEditingProjectMaterialsResponse;
import com.aliyun.ice20201109.models.GetEditingProjectRequest;
import com.aliyun.ice20201109.models.GetEditingProjectResponse;
import com.aliyun.ice20201109.models.SearchEditingProjectRequest;
import com.aliyun.ice20201109.models.SearchEditingProjectResponse;
import com.aliyun.ice20201109.models.SubmitMediaProducingJobRequest;
import com.aliyun.ice20201109.models.SubmitMediaProducingJobResponse;
import com.aliyun.ice20201109.models.UpdateEditingProjectRequest;
import com.aliyun.ice20201109.models.UpdateEditingProjectResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.AddEditingProjectMaterialsReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.AddEditingProjectMaterialsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.CreateEditingProjectReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.CreateEditingProjectRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.DeleteEditingProjectMaterialsReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.DeleteEditingProjectMaterialsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.DeleteEditingProjectsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.EditingProjectMaterialsRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.GetEditingProjectReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.GetEditingProjectRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.SearchEditingProjectReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.SearchEditingProjectRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.SubmitMediaProducingJobReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.SubmitMediaProducingJobRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.UpdateEditingProjectReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.production.vo.UpdateEditingProjectRespVO;
import com.hsjk.szwj.module.smedia.convert.production.CloudEditConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * @desc 云剪辑相关接口实现类
 * @Date 2023/07/06 09:34
 **/
@Service
@Validated
@Slf4j
public class CloudEditImpl implements ICloudEdit {
    @Value("${ali.media.access-key-id}")
    private String accessKeyId;

    @Value("${ali.media.access-key-secret}")
    private String accessKeySecret;

    @Value("${ali.media.endpoint}")
    private String endpoint;

    @Override
    public EditingProjectMaterialsRespVO getEditingProjectMaterials(String projectId) {
        if (ObjectUtil.isNull(projectId)) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                    , "参数不能为空");
        }
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        EditingProjectMaterialsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetEditingProjectMaterialsRequest request = new GetEditingProjectMaterialsRequest();
            request.setProjectId(projectId);
            GetEditingProjectMaterialsResponse materialsResponse = client.getEditingProjectMaterialsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(materialsResponse)) {
                response = CloudEditConvert.INSTANCE.convertEditingProjectMaterialsRespVO(materialsResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public CreateEditingProjectRespVO createEditingProject(CreateEditingProjectReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        CreateEditingProjectRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            CreateEditingProjectRequest request = new CreateEditingProjectRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getClipsParam())) {
                request.setClipsParam(reqVO.getClipsParam());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getBusinessConfig())) {
                request.setBusinessConfig(reqVO.getBusinessConfig());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getCoverUrl())) {
                request.setCoverURL(reqVO.getCoverUrl());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getProjectType())) {
                request.setProjectType(reqVO.getProjectType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getDescription())) {
                request.setDescription(reqVO.getDescription());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getMaterialMaps())) {
                request.setMaterialMaps(reqVO.getMaterialMaps());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateId())) {
                request.setTemplateId(reqVO.getTemplateId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTimeline())) {
                request.setTimeline(reqVO.getTimeline());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTitle())) {
                request.setTitle(reqVO.getTitle());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateType())) {
                request.setTemplateType(reqVO.getTemplateType());
            }

            CreateEditingProjectResponse projectResponse = client.createEditingProjectWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(projectResponse)) {
                response = CloudEditConvert.INSTANCE.convertCreateEditingProjectRespVO(projectResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public AddEditingProjectMaterialsRespVO addEditingProjectMaterials(AddEditingProjectMaterialsReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        AddEditingProjectMaterialsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            AddEditingProjectMaterialsRequest request = new AddEditingProjectMaterialsRequest();
            request.setProjectId(reqVO.getProjectId());
            request.setMaterialMaps(reqVO.getMaterialMaps());
            AddEditingProjectMaterialsResponse materialsResponse = client.addEditingProjectMaterialsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(materialsResponse)) {
                response = CloudEditConvert.INSTANCE.convertAddEditingProjectMaterialsRespVO(materialsResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public DeleteEditingProjectMaterialsRespVO deleteEditingProjectMaterials(DeleteEditingProjectMaterialsReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        DeleteEditingProjectMaterialsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            DeleteEditingProjectMaterialsRequest request = new DeleteEditingProjectMaterialsRequest();
            request.setProjectId(reqVO.getProjectId());
            request.setMaterialIds(reqVO.getMaterialIds());
            request.setMaterialType(reqVO.getMaterialType());
            DeleteEditingProjectMaterialsResponse materialsResponse = client.deleteEditingProjectMaterialsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(materialsResponse)) {
                response = CloudEditConvert.INSTANCE.convertDeleteEditingProjectMaterialsRespVO(materialsResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public GetEditingProjectRespVO getEditingProject(GetEditingProjectReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        GetEditingProjectRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetEditingProjectRequest request = new GetEditingProjectRequest();
            request.setProjectId(reqVO.getProjectId());
            request.setRequestSource(reqVO.getRequestSource());
            GetEditingProjectResponse projectResponse = client.getEditingProjectWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(projectResponse)) {
                response = CloudEditConvert.INSTANCE.convertGetEditingProjectRespVO(projectResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public UpdateEditingProjectRespVO updateEditingProject(UpdateEditingProjectReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        UpdateEditingProjectRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            UpdateEditingProjectRequest request = new UpdateEditingProjectRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getProjectId())) {
                request.setProjectId(reqVO.getProjectId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getClipsParam())) {
                request.setClipsParam(reqVO.getClipsParam());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getCoverUrl())) {
                request.setCoverURL(reqVO.getCoverUrl());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getDescription())) {
                request.setDescription(reqVO.getDescription());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTimeline())) {
                request.setTimeline(reqVO.getTimeline());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getBusinessStatus())) {
                request.setBusinessStatus(reqVO.getBusinessStatus());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateId())) {
                request.setTemplateId(reqVO.getTemplateId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTitle())) {
                request.setTitle(reqVO.getTitle());
            }

            UpdateEditingProjectResponse projectResponse = client.updateEditingProjectWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(projectResponse)) {
                response = CloudEditConvert.INSTANCE.convertUpdateEditingProjectRespVO(projectResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public SubmitMediaProducingJobRespVO submitMediaProducingJob(SubmitMediaProducingJobReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        SubmitMediaProducingJobRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            SubmitMediaProducingJobRequest request = new SubmitMediaProducingJobRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getProjectId())) {
                request.setProjectId(reqVO.getProjectId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getClipsParam())) {
                request.setClipsParam(reqVO.getClipsParam());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getClientToken())) {
                request.setClientToken(reqVO.getClientToken());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateId())) {
                request.setTemplateId(reqVO.getTemplateId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTimeline())) {
                request.setTimeline(reqVO.getTimeline());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getEditingProduceConfig())) {
                request.setEditingProduceConfig(reqVO.getEditingProduceConfig());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getProjectMetadata())) {
                request.setProjectMetadata(reqVO.getProjectMetadata());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getSource())) {
                request.setSource(reqVO.getSource());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getUserData())) {
                request.setUserData(reqVO.getUserData());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getMediaMetadata())) {
                request.setMediaMetadata(reqVO.getMediaMetadata());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getOutputMediaConfig())) {
                request.setOutputMediaConfig(reqVO.getOutputMediaConfig());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getOutputMediaTarget())) {
                request.setOutputMediaTarget(reqVO.getOutputMediaTarget());
            }
            SubmitMediaProducingJobResponse jobResponse = client.submitMediaProducingJobWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(jobResponse)) {
                response = CloudEditConvert.INSTANCE.convertSubmitMediaProducingJobRespVO(jobResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public SearchEditingProjectRespVO searchEditingProject(SearchEditingProjectReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        SearchEditingProjectRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            SearchEditingProjectRequest request = new SearchEditingProjectRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getProjectType())) {
                request.setProjectType(reqVO.getProjectType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTemplateType())) {
                request.setTemplateType(reqVO.getTemplateType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getCreateSource())) {
                request.setCreateSource(reqVO.getCreateSource());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getEndTime())) {
                request.setEndTime(reqVO.getEndTime());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getPageNo())) {
                request.setPageNo(reqVO.getPageNo());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getPageSize())) {
                request.setPageSize(reqVO.getPageSize());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getSortBy())) {
                request.setSortBy(reqVO.getSortBy());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getStartTime())) {
                request.setStartTime(reqVO.getStartTime());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getStatus())) {
                request.setStatus(reqVO.getStatus());
            }
            SearchEditingProjectResponse projectResponse = client.searchEditingProjectWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(projectResponse)) {
                response = CloudEditConvert.INSTANCE.convertSearchEditingProjectRespVO(projectResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public DeleteEditingProjectsRespVO deleteEditingProjects(String projectIds) {
        if (ObjectUtil.isNull(projectIds)) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                    , "参数不能为空");
        }
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        DeleteEditingProjectsRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            DeleteEditingProjectsRequest request = new DeleteEditingProjectsRequest();
            request.setProjectIds(projectIds);
            DeleteEditingProjectsResponse projectResponse = client.deleteEditingProjectsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(projectResponse)) {
                response = CloudEditConvert.INSTANCE.convertDeleteEditingProjectsRespVO(projectResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

}
